package com.stox.ui.javafx.core.widget.wrapper;

import javafx.scene.Node;

public class TreeItem<T> extends javafx.scene.control.TreeItem<T> {

	public TreeItem() {
	}

	public TreeItem(T paramT) {
		super(paramT);
	}
	
	public TreeItem(T paramT, Node paramNode) {
		super(paramT, paramNode);
	}

	public TreeItem<T> nextTreeItem(){
		if(getChildren().isEmpty()) {
			TreeItem<T> nextSibling = (TreeItem<T>)nextSibling();
			TreeItem<T> parent = (TreeItem<T>)getParent();
			if(nextSibling == null) {
				do {
					if(parent == null) return null;
					nextSibling = (TreeItem<T>)parent.nextSibling();
					if(nextSibling != null) return nextSibling;
					parent = (TreeItem<T>)parent.getParent();
				}while(true);
			}else return nextSibling;
		}else return (TreeItem<T>)getChildren().get(0);
	}

	public TreeItem<T> previousTreeItem(){
		TreeItem<T> parent = (TreeItem<T>)getParent();
		if(parent == null) return null;
		TreeItem<T> previousSibling = (TreeItem<T>)previousSibling();
		if(previousSibling != null) {
			if(previousSibling.getChildren().size() == 0) {
				return previousSibling;
			}else {
				return previousSibling.getLastLeaf();
			}
		}else return parent;
	}
	
	public TreeItem<T> getLastLeaf(){
		TreeItem<T> treeItem = this;
		while(!treeItem.isLeaf()) treeItem = treeItem.getLastChild();
		return treeItem;
	}
	
	public TreeItem<T> getLastChild(){
		if(getChildren().size() == 0) return null;
		else return (TreeItem<T>)getChildren().get(getChildren().size() - 1);
	}
	
	public void expand() {
		if(null != getParent()) ((TreeItem<T>)getParent()).expand(); 
		if(!isLeaf()) setExpanded(true);
	}
	
}
